package com.example.side_pull_menu.Fragment;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.side_pull_menu.MySqliteOpenHelper;
import com.example.side_pull_menu.R;
import com.example.side_pull_menu.Traffic_lights;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class Fragment_6 extends Fragment{
    private Button query;
    private SQLiteDatabase db;
    private Button batch_setup;
    private TextView tv_intersection;
    private TextView red_light;
    private TextView green_light;
    private TextView yellow_light;
    private CheckBox operation_item;
    private Button bu_set_up;
    private Spinner spinner;
    private ListView lv;;
    private List<Traffic_lights>Traffic_lights1=new ArrayList<>();
    public Fragment_6() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.traffic_lights, container, false);
        initView(view);
        initData();
        query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             lv.setAdapter(new TlAdapter());
            }
        });
        return view;
    }
    public interface BlogService{
        @GET("getTrafficLightRGY")
        Call<ResponseBody>getBlong();
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initView(View view) {
        spinner=(Spinner)view.findViewById(R.id.spinner);
        lv=(ListView)view.findViewById(R.id.lv);
        query = (Button) view.findViewById(R.id.query);
        batch_setup = (Button) view.findViewById(R.id.batch_setup);
        tv_intersection = (TextView) view.findViewById(R.id.tv_intersection);
        red_light = (TextView) view.findViewById(R.id.red_light);
        green_light = (TextView) view.findViewById(R.id.green_light);
        yellow_light = (TextView) view.findViewById(R.id.yellow_light);
        operation_item = (CheckBox) view.findViewById(R.id.operation_item);
        bu_set_up = (Button) view.findViewById(R.id.bu_set_up);
    }
    private void initData(){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogService service=retrofit.create(BlogService.class);
        Call<ResponseBody> call=service.getBlong();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Gson gson = new Gson();
                    Traffic_lights carlist = gson.fromJson(response.body().string(),Traffic_lights.class);
                    Traffic_lights1.add(carlist);
                            //spinner
                            final String[] sspinner=getResources().getStringArray(R.array.letter);
                            ArrayAdapter<String> adapter1=new ArrayAdapter<String>(getActivity(),android.R.layout.test_list_item,sspinner);
                            spinner.setAdapter(adapter1);
                            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                   switch (i){
                                       case 0:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort2());
                                           break;
                                       case 1:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort1());
                                           break;
                                       case 2:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort3());
                                           break;
                                       case 3:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort4());
                                           break;
                                       case 4:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort5());
                                           break;
                                       case 5:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort6());
                                           break;
                                       case 6:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort7());
                                           break;
                                       case 7:
                                           Collections.sort(Traffic_lights1.get(0).getData(), new sort8());
                                           break;
                                   }
                                }
                                @Override
                                public void onNothingSelected(AdapterView<?> adapterView) {
                        }
                    });
//                    Collections.sort(Traffic_lights1.get(0).getData(), new sort2());
//                    Collections.sort(Traffic_lights1.get(0).getData(), new sort1());
                    lv.setAdapter(new TlAdapter());
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }
    class TlAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return Traffic_lights1.get(0).getData().size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1=(View)View.inflate(getContext(),R.layout.fragment_6,null);
            TextView tv_intersection=view1.findViewById(R.id.tv_intersection);
            TextView red_light=view1.findViewById(R.id.red_light);
            TextView green_light=view1.findViewById(R.id.green_light);
            TextView yellow_light=view1.findViewById(R.id.yellow_light);
            tv_intersection.setText(Traffic_lights1.get(0).getData().get(i).getRoad()+"");
            red_light.setText(Traffic_lights1.get(0).getData().get(i).getRed()+"");
            green_light.setText(Traffic_lights1.get(0).getData().get(i).getGreen()+"");
            yellow_light.setText(Traffic_lights1.get(0).getData().get(i).getYellow()+"");
            return view1;
        }
    }
    public class sort1 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl2.getRoad()-tl1.getRoad();
        }
    }
    public class sort2 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl1.getRoad()-tl2.getRoad();
        }
    }
    public class sort3 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl1.getRed()-tl2.getRed();
        }
    }
    public class sort4 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl2.getRed()-tl1.getRed();
        }
    }
    public class sort5 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl1.getGreen()-tl2.getGreen();
        }
    }
    public class sort6 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl2.getGreen()-tl1.getGreen();
        }
    }
    public class sort7 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl1.getYellow()-tl2.getYellow();
        }
    }
    public class sort8 implements Comparator<Traffic_lights.DataBean>{
        @Override
        public int compare(Traffic_lights.DataBean tl1, Traffic_lights.DataBean tl2) {
            return tl2.getYellow()-tl1.getYellow();
        }
    }
}
